//
//  VisitorView.swift
//  LVWeibo
//
//  Created by lv on 15/6/23.
//  Copyright © 2015年 lv. All rights reserved.
//

import UIKit
import FFAutoLayout

// 访客视图协议
protocol VisitorViewDelegate :NSObjectProtocol {
    // 协议方法
    func visitorRegisterButtonClick()
    func visitorLoginButtonClick()
}
class VisitorView: UIView {
    weak var delegate :VisitorViewDelegate?

    //MARK:- 用户注册
    func register(){
        delegate?.visitorRegisterButtonClick()
    }
    
    func login(){
        delegate?.visitorLoginButtonClick()
    }
    
    
    //MARK:- 设置视图显示内容(视图切换内容变化)
    func setUpViewInfo(iconName:String,message:String,isHome:Bool){
    
        // 设置消息文本内容显示
        messageLable.text = message
        
        // 判断是否为首页
        if isHome {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        }else{
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            // 将视图层次置后（遮罩）
            sendSubviewToBack(maskIconView)
        }
    }
    
    //MARK:- 开始动画（首页iconView转动效果）
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    // 视图默认的构造方法
    // 创建控件 & 设置布局在次方法中实现
    // 使用sb & xib不会调用此方法
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.lightGrayColor()
        
        // 视图中添加子控件并设置布局
        setUpSubViews()
    }
    
    // MARK:- 创建控件&设置布局
    private func setUpSubViews() {
    
        // 初始化时添加视图控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 自动布局
        iconView.ff_AlignHorizontal(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -80))
        // 房子
        homeIconView.ff_AlignHorizontal(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        // 消息文本
        messageLable.ff_AlignHorizontal(ff_AlignType.CenterCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0, y: 120))
        // 注册按钮
        registerBtn.ff_AlignInner(ff_AlignType.TopLeft, referView: messageLable, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 50))
        // 登陆按钮
        loginBtn.ff_AlignInner(ff_AlignType.BottomRight, referView: messageLable, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 50))
        // 遮罩
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 240, right: 0))
        // 背景色设置（补齐遮罩下方颜色）
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    // 解档，用 xib & sb 开发会调用
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///MARK: - 懒加载视图控件
    lazy var iconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    // 遮罩图片
    lazy var maskIconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    /// 房子
    lazy var homeIconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    /// 消息文本
    lazy var messageLable:UILabel = {
        let lable = UILabel()
        lable.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(14)
        lable.textAlignment = NSTextAlignment.Center
        lable.preferredMaxLayoutWidth = 224
        // 最多显示两行，超出部分显示...
        lable.numberOfLines = 2
        lable.sizeToFit()
        return lable
    }()
    
    ///  注册按钮
    lazy var registerBtn:UIButton = {
        let btn = UIButton(frame: CGRectMake(230, 37, 100, 40))
        // 设置按钮文字
        btn.setTitle("注册", forState: UIControlState.Normal)
        // 设置文字颜色
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        // 设置按钮背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    ///  登录按钮
    lazy var loginBtn:UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 300, 100, 40))
        btn.setTitle("登录", forState: UIControlState.Normal)
        // 设置文字颜色
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
